﻿using System;

namespace MC.EventHandler.Install
{
    public class InstallationConfiguration
    {
        private InstallationConfiguration()
        {
        }

        public static InstallationConfiguration Parse(string[] args)
        {
            var result = new InstallationConfiguration();
            for (int i = 0; i < args.Length; i++)
            {
                switch (args[i])
                {
                    case @"/SQLServer":
                        if(i+1>=args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"SQL Server Name should be specified after the /SQLServer parameter");
                        }
                        result.SQLServer = args[++i];
                        break;
                    case @"/TestData":
                        result.TestData = true;
                        break;
                    case @"/AdminDbUsername":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Admin Database Username should be specified after the /AdminDbUsername parameter");
                        }
                        result.AdminDbUsername = args[++i];
                        break;
                    case @"/AdminDbPassword":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Admin Database Password should be specified after the /AdminDbPassword parameter");
                        }
                        result.AdminDbPassword = args[++i];
                        break;
                    case @"/AppDbUsername":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Application Database Username should be specified after the /AppDbUsername parameter");
                        }
                        result.AppDbUsername = args[++i];
                        break;
                    case @"/AppDbPassword":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Application Database Password should be specified after the /AppDbPassword parameter");
                        }
                        result.AppDbPassword = args[++i];
                        break;
                    case @"/DBName":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Database Name should be specified after the /DBName parameter");
                        }
                        result.DBName = args[++i];
                        break;
                    case @"/AppAdminUsername":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Application Admin Username should be specified after the /AppAdminUsername parameter");
                        }
                        result.AppAdminUsername = args[++i];
                        break;
                    case @"/AppAdminPassword":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Application Admin Password should be specified after the /AppAdminPassword parameter");
                        }
                        result.AppAdminPassword = args[++i];
                        break;
                    case @"/VirtualDirectoryName":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Virtual Directory Name should be specified after the /VirtualDirectoryName parameter");
                        }
                        result.VirtualDirectoryName = args[++i];
                        break;
                    case @"/WebDirectory":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"Web Directory Name should be specified after the /WebDirectory parameter");
                        }
                        result.WebDirectory = args[++i];
                        break;
                    case @"/NewWebApplication":
                        result.NewWebApplication = true;
                        break;
                    case @"/SMTPHost":
                        if (i + 1 >= args.Length)
                        {
                            throw new ArgumentOutOfRangeException("args", @"SMTP Host should be specified after the /SMTPHost parameter");
                        }
                        result.SMTPHost = args[++i];
                        break;
                }
            }
            return result;
        }

        public bool TestData { get; set; }

        public string SQLServer { get; set; }

        public string AdminDbUsername { get; set; }

        public string AdminDbPassword { get; set; }

        public string AppDbUsername { get; set; }

        public string AppDbPassword { get; set; }

        public string DBName { get; set; }

        public string AppAdminUsername { get; set; }

        public string AppAdminPassword { get; set; }

        public string VirtualDirectoryName { get; set; }

        public bool NewWebApplication { get; set; }

        public string SMTPHost { get; set; }

        private string WebDirectory { get; set; }

        public bool AnyRequiredValuesBlank()
        {
            return String.IsNullOrEmpty(SQLServer) ||
                   String.IsNullOrEmpty(AdminDbUsername) ||
                   String.IsNullOrEmpty(AdminDbPassword) ||
                   String.IsNullOrEmpty(AppDbUsername) ||
                   String.IsNullOrEmpty(AppDbPassword) ||
                   String.IsNullOrEmpty(DBName) ||
                   String.IsNullOrEmpty(AppAdminUsername) ||
                   String.IsNullOrEmpty(AppAdminPassword) ||
                   String.IsNullOrEmpty(VirtualDirectoryName) ||
                   string.IsNullOrEmpty(WebDirectory)||
                   String.IsNullOrEmpty(SMTPHost);
                
        }

    }
}
